#pragma once 
#include <iostream>
#include <cstring>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>

namespace my_sock
{
  class Sock 
  {
    public:
      static int Socket()
      {
        int sock =socket(AF_INET,SOCK_STREAM,0);
        if(sock<0)
        {
          std::cerr<<"socket error"<<std::endl;
          exit(1);
        }
        int opt=1;
        setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));

      return sock; 
      }

      static bool Bind(int sock, unsigned short port)
      {
        struct sockaddr_in local;
        memset(&local,0,sizeof(local));
        local.sin_family=AF_INET;
        local.sin_port=htons(port);
        local.sin_addr.s_addr=0;

        if(bind(sock,(struct sockaddr*)&local,sizeof(local))<0)
        {
          std::cerr<<"bind error"<<std::endl;
          exit(2);
        }
        return true;
      }

      static bool Listen(int sock,int size)
      {
        if(listen(sock,size)<0)
        {
          std::cerr<<"listen error"<<std::endl;
          exit(3);
        }
        return true;
      }
  };
}
